package com.xuecheng.media;

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import io.minio.*;
import io.minio.errors.*;
import lombok.ToString;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.awt.*;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Filter;

/**
 * @author 冯佳鑫 又菜又爱学
 * @version 1.0
 * @DATE now()
 */
@SpringBootTest
public class miniotest {
    MinioClient minioClient =
            MinioClient.builder()
                    .endpoint("http://192.168.101.65:9000/")
                    .credentials("minioadmin", "minioadmin").build();

 //上传文件
   @Test
public void upload() throws IOException, ServerException, InsufficientDataException, InternalException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException, ErrorResponseException {
       ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(".xls");
       String mimeType = extensionMatch.getMimeType();
       UploadObjectArgs build = UploadObjectArgs.builder().bucket("testbucket").filename("C:\\Users\\net58\\Desktop\\money.xls").object("money").contentType(mimeType).build();
       minioClient.uploadObject(build);
   }
@Test
    public void delete() throws IOException, ServerException, InsufficientDataException, InternalException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException, ErrorResponseException {


        minioClient.removeObject(RemoveObjectArgs.builder().bucket("testbucket").object("fine.txt").build());
    }
  @Test
    //查询文件
    public void query() throws IOException, ServerException, InsufficientDataException, InternalException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException, ErrorResponseException {
        GetObjectArgs testbucket = GetObjectArgs.builder().bucket("testbucket").object("money").build();
        FilterInputStream inputStream = minioClient.getObject(testbucket);
        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\net58\\Desktop\\ok.xls"));
        IOUtils.copy(inputStream,fileOutputStream);
        //校验
        FileInputStream inputStream1 = new FileInputStream(new File("C:\\Users\\net58\\Desktop\\ok.xls"));
        FileInputStream inputStream2 = new FileInputStream("C:\\Users\\net58\\Desktop\\money.xls");
        String s = DigestUtils.md5Hex(inputStream1);
        String s1 = DigestUtils.md5Hex(inputStream2);
        if (s.equals(s1)){
            System.out.println("传输成功");
        }
    }

}